package cn.e4j.modular.crm.service.impl;

import cn.e4j.core.dto.impl.Dto;
import cn.e4j.core.shiro.ShiroKit;
import cn.e4j.core.shiro.ShiroUser;
import cn.e4j.core.util.AppCxt;
import cn.e4j.core.util.InvokeUtil;
import cn.e4j.modular.common.service.SysAuthorityService;
import cn.e4j.modular.crm.entity.GwdgT;
import cn.e4j.modular.crm.mapper.GwdgTMapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Map;

/**
 * <p>
 *  外部返利维护 服务实现类
 * </p>
 *
 * @author miaoyl
 * @since 2019-04-29
 */
@Service
public class GwdgTServiceImpl extends ServiceImpl<GwdgTMapper, GwdgT> {

    @Autowired
    public SysAuthorityService sysAuthorityService;

    @Transactional(readOnly = true)
    public List<Map<String,Object>> getGwdglist(Page<Object> page, Dto dto) {
        //客户权限
        dto.put("CUSTOMERS", sysAuthorityService.getCustomersAuthority(dto));
//        List<Map<String,Object>> gwdgData = this.baseMapper.listGwdg(page, dto);
        List<Map<String,Object>> gwdgData = InvokeUtil.invokeBeanMethod("gwdgTMapper,listGwdg",page,dto);
        return gwdgData;
    }

    @Transactional(readOnly = true)
    public List<Dto> getGwdelist(Dto dto) {
        List<Dto> gwdeData = this.baseMapper.listGwde(dto);
        return gwdeData;
    }

    /**
     * 增加
     */
    @Transactional
    public void add(GwdgT gwdgT) {
        ShiroUser shiroUser = ShiroKit.getUser();
        gwdgT.setGWDGDOCNO(AppCxt.timeId());
        gwdgT.setGWDGACTI("Y");
        gwdgT.setGWDGENT(shiroUser.getDBC());
        gwdgT.insert();
    }

    /**
     * 修改
     *
     * @param
     * @return
     */
    @Transactional
    public void edit(GwdgT gwdgT) {
        UpdateWrapper<GwdgT> wrapper = new UpdateWrapper<>();
        wrapper.eq("GWDGENT", gwdgT.getGWDGENT());
        wrapper.eq("GWDGDOCNO", gwdgT.getGWDGDOCNO());
        gwdgT.update(wrapper);
    }

    /**
     * 删除
     */
    @Transactional
    public void del(GwdgT gwdgT) {
        UpdateWrapper<GwdgT> wrapper = new UpdateWrapper<>();
        wrapper.eq("GWDGENT", gwdgT.getGWDGENT());
        wrapper.eq("GWDGDOCNO", gwdgT.getGWDGDOCNO());
        gwdgT.delete(wrapper);
    }

    @Transactional
    public void deleteDataBatch(List<GwdgT> gwdgTList) {
        for (GwdgT gwdgT : gwdgTList) {
            UpdateWrapper<GwdgT> wrapper = new UpdateWrapper<>();
            wrapper.eq("GWDGENT", gwdgT.getGWDGENT());
            wrapper.eq("GWDGDOCNO", gwdgT.getGWDGDOCNO());
            gwdgT.delete(wrapper);
        }
    }
}
